import store from "@/store"
class ApiHelper {
  myFetch(url,method,data) {
	  let token=uni.getStorageSync('openid');
	  // store.state.token
	  uni.showLoading({
	  	title: '加载中',
	  	mask: true
	  });
    return new Promise((resolve, reject) => {
     // const { data, url } = requestParam;
	 console.log("apiHelper.fetch:",url,data)
      wx.request({
        url,
        method: method,
        data: data,
		dataType: "json",
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          "token":token
        },
        success: (res) => {
          uni.hideLoading();
          let ret = res.data.code;
          let message = res.data.msg;
          resolve(res.data);

          if (ret != 0) {
            wx.showToast({
              title: message,
              icon: 'none',
              duration: 2000
            });
          }
		   
		   if(ret=='4012'){
			  uni.navigateTo({
			  	url:'/pages/mes/login'
			  })
		  }
        },
        fail: (e) => {
			uni.hideLoading();
			uni.showToast({
				title: "网络异常，请稍后再试!",
				icon: "none",
			})
			reject("网络异常，请稍后再试!");
          
        },
      });
    });
  }
  
  myBackFetch(url,method,data) {
  	  let token=uni.getStorageSync('token');
  	  // store.state.token
  	 
    return new Promise((resolve, reject) => {
     // const { data, url } = requestParam;
  	 console.log("apiHelper.fetch:",url,data)
      wx.request({
        url,
        method: method,
        data: data,
  		dataType: "json",
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          "token":token
        },
        success: (res) => {
          uni.hideLoading();
          let ret = res.data.code;
          let message = res.data.msg;
          resolve(res.data);
  
          if (ret != 0) {
            wx.showToast({
              title: message,
              icon: 'none',
              duration: 2000
            });
          }
  		   
  		   if(ret=='4012'){
  			  uni.navigateTo({
  			  	url:'/pages/mes/login'
  			  })
  		  }
        },
        fail: (e) => {
  			
  			uni.showToast({
  				title: "网络异常，请稍后再试!",
  				icon: "none",
  			})
  			reject("网络异常，请稍后再试!");
          
        },
      });
    });
  }

  myUploader(requestParam) {
    return new Promise((resolve, reject) => {
      const { filePath, url, name, formData = {} } = requestParam;
      console.log("myUploader==>", url, formData, filePath);
      wx.uploadFile({
        url: url,
        filePath: filePath,
        name: name,
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'sessionId': getApp().globalData.memberId
        },
        formData: formData,
        success: (res) => {
          console.log("apiHelper.fetch:", requestParam, res)
          let ret = res.data.code;
          let message = res.data.message;

          console.log("apiHelper.fetch code:", ret)
          resolve(JSON.parse(res.data));

          if (ret != 0) {
            wx.showToast({
              title: message,
              icon: 'none',
              duration: 2000
            });
          }
        },
        fail: function (res) {
          reject(res)
        }
      })
    });
  }


  request = (url, needSubDomain, method, data) => {
    //let _url = API_BASE_URL + (needSubDomain ? '/' + CONFIG.subDomain : '') + url
    let _url = API_BASE_URL + '/' + url
    return new Promise((resolve, reject) => {
      wx.request({
        url: _url,
        method: method,
        data: data,
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'sessionId': getApp().globalData.memberId
        },
        success(res) {
          console.log(res)
          let ret = res.data.code;
          let message = res.data.message;
          resolve(res.data);
          if (ret != 0) {
            wx.showToast({
              title: message,
              icon: 'none',
              duration: 2000
            });
          }
        },
        fail(error) {
          reject(error)
        },
        complete(aaa) {
          // 加载完成
        }
      })
    })
  }
}

export default new ApiHelper();
